﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace XF.Common
{
    public class IOExtension
    {
        /// <summary>
        /// 组成文件目录
        /// </summary>
        /// <param name="MakeDir">创建该目录(仅本机有效)</param>
        /// <param name="dir">目录拼接</param>
        /// <returns></returns>
        public static string MakeDir(bool MakeDir, params string[] dir)
        {
            string retstr = "";
            foreach (string s in dir)
            {
                string str = fixdir(s);
                //首个节点
                if (string.IsNullOrEmpty(retstr))
                {
                    retstr += str;
                }
                else
                {
                    if (str.StartsWith(@"\"))
                        retstr += str;
                    else
                        retstr += @"\" + str;
                }
            }
            retstr += @"\";
            if (MakeDir)
            {
                if (!Directory.Exists(retstr))
                {
                    Directory.CreateDirectory(retstr);
                }
            }
            return retstr;
        }

        /// <summary>
        /// 规范格式,去掉尾部
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        static string fixdir(string str)
        {
            if (string.IsNullOrEmpty(str))
                return "";
            string s = str.Replace("/", @"\");
            s = s.Replace(@"\\", @"\");
            if (s.IndexOf(@"\\") >= 0)
            {
                s = fixdir(s);
            }
            if (s.EndsWith(@"\"))
            {
                s = s.Substring(0, s.Length - 1);
            }
            return s;
        }
    }
}
